package com.example.smartf.schedule;

import com.example.smartf.model.HistoryData;
import com.example.smartf.model.mysql.Deviceinfo;
import com.example.smartf.model.mysql.Traindata;
import com.example.smartf.model.mysql.TraindataKey;
import com.example.smartf.repository.mysql.DeviceinfoRepository;
import com.example.smartf.repository.mysql.TraindataRepository;
import com.example.smartf.tool.influxdb.InfluxdbDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
@EnableScheduling
public class TrainDataSaver {

    @Autowired
    DeviceinfoRepository deviceinfoRepository;
    @Autowired
    InfluxdbDao influxdbDao;
    @Autowired
    TraindataRepository traindataRepository;

    Logger logger = LoggerFactory.getLogger(getClass());

    //每天              0秒 0分 0时 执行
    @Scheduled(cron = "0 0 0 * * ?")
    public void DayChangeTrainDataSave(){
        List<Deviceinfo> list = deviceinfoRepository.findAll();
        for (Deviceinfo deviceinfo : list){
            //若 deviceinfo 中 startday 不为空 则表示 设备是使用状态 则 进行训练数据统计
            if( "".equals(deviceinfo.getStartday()) == true || deviceinfo.getStartday() == null || deviceinfo.getStartday().length() == 0){
                continue;
            }else {
                //取出 要查询的 起始时间 和 终止时间
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date startdate = new Date();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startdate);
                calendar.add(Calendar.DAY_OF_MONTH,-1);
                startdate = calendar.getTime();
                Date enddate = new Date();
                List<HistoryData> historyDatas = influxdbDao.findByTime(sdf.format(startdate),sdf.format(enddate),deviceinfo.getDeviceid());
                //若无 历史数据
                if(historyDatas.size() == 0){

                    continue;
                }

                sdf = new SimpleDateFormat("yyyy-MM-dd");
                Integer days = new Integer(0);
                try {
                    days = (int)( (new Date().getTime() - sdf.parse(deviceinfo.getStartday()).getTime()) / (24*3600*1000) );
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //计算 每天的各项平均值
                Double co2 = new Double(0),
                        ec = new Double(0) ,
                        ph = new Double(0),
                        rh = new Double(0),
                        temp = new Double(0) ,
                        lighttime = new Double(0),
                        light = new Double(0);
                for( HistoryData historyData : historyDatas){
                    co2 += Double.parseDouble(historyData.getCO2());
                    ec += Double.parseDouble(historyData.getEC());
                    ph += Double.parseDouble(historyData.getPH());
                    rh += Double.parseDouble(historyData.getRH());
                    temp += Double.parseDouble(historyData.getTEMP());
                    light += Double.parseDouble(historyData.getLIGHT());
                    if( "1".equals(historyData.getLED()) == true){
                        lighttime = lighttime + 1.0;
                    }
                }
                if(historyDatas.size() == 0){
                    continue;
                }
                co2 = co2 / historyDatas.size();
                ec = ec /historyDatas.size();
                ph = ph / historyDatas.size();
                rh = rh / historyDatas.size();
                temp = temp / historyDatas.size();
                lighttime = lighttime * 30;
                light = light / historyDatas.size();

                //构造 训练数据 一条
                TraindataKey traindataKey = new TraindataKey();
                traindataKey.setDeviceID(deviceinfo.getDeviceid());
                traindataKey.setDays(days);
                Traindata traindata = new Traindata(traindataKey,co2,ec,ph,rh,temp,lighttime,light);

                logger.info("设备ID：" + deviceinfo.getDeviceid() + "定时记录训练数据：" + traindata.toString() );

                traindataRepository.save(traindata);

            }
        }
    }
}
